/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import java.util.ArrayList;
import model.db.UsersDB;

/**
 *
 * @author user
 */
public class Rooms extends ArrayList<Room> {

    public Room find(int id) {
        for (int i = 0; i < this.size(); ++i) {
            if (this.get(i).id == id) {
                return this.get(i);
            }
        }

        return null;
    }

    public Room find(String name) {
        for (int i = 0; i < this.size(); ++i) {
            if (this.get(i).name.equals(name)) {
                return this.get(i);
            }
        }

        return null;
    }

    public void removeJoinUser(int userId, int roomId) {
        Room room = find(roomId);

        if (room != null) {
            room.removeJoinUser(userId);
            if (room.joinUsers.size() < 1 && room.persistance == false) {
                remove(room);
            } else {
                if (room.ownerId == userId) {
                    // Set owner (belum memperhatikan jumlah room yang dimiliki)
                    room.ownerId = room.joinUsers.get(0).id;
                }
            }
        }
    }

    public void addBanUser(User user, int roomId) {
        // find room
        // if not null
        // call room.addBanUser
    }

    public void removeBanUser(String username, int roomId) {
        // find room
        // if room not null
        // call romm.removeBanUser
    }

    public void markKickToUser(String username, int roomId) {
        // find room
        // if not null
        // call room.markKickToUser
    }

    public void setPersist(boolean val, int roomId) {
    }

    public void setOwner(String username, int roomId) {
        UsersDB usersDB = new UsersDB();
        User user = usersDB.find(username);
        Room room = find(roomId);
        
        if (room != null && user != null) {
            room.ownerId = user.id;
        }
    }

    public boolean isKickAndRemove(int userId, int roomId) {
        Room room = find(roomId);
        if(room != null) {
            User user = room.joinUsers.find(userId);
            if (user != null && user.kick) {
                removeJoinUser(userId, roomId);
                return true;
            }
        }
        return false;
    }
}
